package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    private CityInfoMapper cityInfoMapper;

    @Autowired
    private DistrictInfoMapper districtInfoMapper;




    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService  apartmentFeeValueService;

    @Autowired
    private GraphInfoService graphInfoService;



    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;


    @Override
    public IPage<ApartmentItemVo> getApartmentItemVo(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getApartmentItemVo(page, queryVo);
    }

    @Override
    public void saveOrUpdateApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();

        String provinceName = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId()).getName();

        String cityName = cityInfoMapper.selectById(apartmentSubmitVo.getCityId()).getName();

        String districtName = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId()).getName();

        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);

        this.saveOrUpdate(apartmentSubmitVo);
        if(id != null){
            //改
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);




            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            facilityInfoIds.forEach(facilityInfoId -> {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(id).facilityId(facilityInfoId).build();
                apartmentFacilities.add(apartmentFacility);
            });
            apartmentFacilityService.saveBatch(apartmentFacilities);


            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            labelIds.forEach(labelId -> {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(id).labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            });
            apartmentLabelService.saveBatch(apartmentLabels);

            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            feeValueIds.forEach(feeValueId -> {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build();
                apartmentFeeValues.add(apartmentFeeValue);
            });
            apartmentFeeValueService.saveBatch(apartmentFeeValues);

            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            graphVoList.forEach(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfos.add(graphInfo);
            });
            graphInfoService.saveBatch(graphInfos);

        }



    }

    @Override
    public ApartmentDetailVo getApartmentDetailVo(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        List<GraphVo> graphVoList = graphInfoMapper.getGraphVoListByApartmentId(id);
        List<LabelInfo> labelInfoList =labelInfoMapper.getLabelInfoListByApartmentId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoListByApartmentId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.getFeeValueVoListByApartmentId(id);

        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;

    }

    @Override
    public void removeApartmentInfoById(Long id) {
        this.removeById(id);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }
}




